package com.hongshan.wallet.core.wallet

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.content.pm.ActivityInfo
import android.databinding.ViewDataBinding
import android.graphics.BitmapFactory
import android.os.Bundle
import cn.bingoogolapple.qrcode.zxing.QRCodeDecoder
import com.alibaba.android.arouter.facade.annotation.Route
import com.hongshan.wallet.BR
import com.hongshan.wallet.R
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.log
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.toast
import com.hongshan.wallet.util.Glide4Engine
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import kotlinx.android.synthetic.main.appbar.*


/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.wallet
 * @author   : zhangxx
 * @date     : 2019/3/26
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */
@Route(path = RouterIndex.BIND_ACCOUNT)
class BindAccountActivity: MyActivity<ViewDataBinding, BindAccountViewModel>() {

    override fun initVariableId() = BR.vm

    override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_bind_account

    override fun initViewModel() = ViewModelProviders.of(this)[BindAccountViewModel::class.java]

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tvTitle.text = viewModel.accountBean?.type_dec
//        val spKey :String
//        val imgRes:ArrayList<Int>
//        if (viewModel.type == Constant.ROUTE_PARAMS_KEY.BIND_ACCOUNT_TYPE_WECHAT) {
//            tvTitle.text = "微信"
//            imgRes = arrayListOf(
//                R.drawable.wx1,
//                R.drawable.wx2,
//                R.drawable.wx3,
//                R.drawable.wx4
//            )
//            spKey = Constant.SP.SP_KEY_FIRST_BIND_WX
//        } else {
//            tvTitle.text = "支付宝"
//            imgRes = arrayListOf(
//                R.drawable.zfb1,
//                R.drawable.zfb2,
//                R.drawable.zfb3
//            )
//            spKey = Constant.SP.SP_KEY_FIRST_BIND_ZFB
//        }
        viewModel.text.set("请确认绑定${tvTitle.text}为您本人账号\n且真实有效")
//        if (MyApplication.sp.getBoolean(spKey,true)){
//            go(RouterIndex.GUIDE, mapOf(
//                Constant.ROUTE_PARAMS_KEY.GUIDE_IMAGE to imgRes,
//                Constant.ROUTE_PARAMS_KEY.GUIDE_NEED_BUTTON to true
//            ))
//        }
//        MyApplication.sp.edit().putBoolean(spKey,false).apply()
        viewModel.openGalley.observe(this, Observer {
            getPicture()
        })
    }

    override fun onStart() {
        super.onStart()
        viewModel.initState()
    }

    fun getPicture() {
        Matisse.from(this)
            .choose(MimeType.ofImage())
            .countable(true)
            .maxSelectable(1)
//            .addFilter(GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
//            .gridExpectedSize(resources.getDimensionPixelSize(R.dimen.grid_expected_size))
            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
            .thumbnailScale(0.85f)
            .imageEngine(Glide4Engine)
            .forResult(REQUEST_CODE_PICTURE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_PICTURE && resultCode == RESULT_OK) {
            val result = Matisse.obtainPathResult(data)
            if (result.isNotEmpty()) {
                val qrCode = QRCodeDecoder.syncDecodeQRCode(BitmapFactory.decodeFile(result[0]))
                viewModel.qrCode = qrCode
                if (qrCode == null) toast("未找到二维码")
                log("QR CODE:$qrCode")
            }
        }
    }

    companion object {
        const val REQUEST_CODE_PICTURE = 10001
    }

}